package com.tony.utils;


import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextUtil {

	public static boolean isEmpty(Object... args) {
		for(int i=0;i<args.length;i++) {
			if(null == args[i] || "".equals((""+args[i]).trim())) {
				return true;
			}
		}
		return false;
	}

	public static boolean isEmpty(String str){
		return str ==null || "".equals(str) || "null".equals(str) || "undefined".equals(str);
	}

	public static boolean isEmpty( CharSequence str) {
		return str == null || str.length() == 0;
	}

	public static String urlEncode(String param) {
		try {
			return URLEncoder.encode(param, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return param;
		}
	}

	public static String urlDecode(String param) {
		try {
			return URLDecoder.decode(param, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
			return param;
		}
	}

	public static void sleep(int s){
		try {
			Thread.sleep(s*1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}


	public static String getTel() {
		String[] telFirst="134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");
		int index=getNum(0,telFirst.length-1);
		String first=telFirst[index];
		String second=String.valueOf(getNum(1,888)+10000).substring(1);
		String third=String.valueOf(getNum(1,9100)+10000).substring(1);
		return first+second+third;
	}
	public static int getNum(int start,int end) {
		return (int)(Math.random()*(end-start+1)+start);
	}


	// 解析验证码短信，获取短信中最长的连续数字
	public static String getNumFromString(String str){
		String result = null;
		if(TextUtil.isEmpty(str)){
			return result;
		}
		Pattern pattern = Pattern.compile("\\d+");
		Matcher matcher = pattern.matcher(str);

		while (matcher.find()) {
			String matched = matcher.group(0);
			if ((result == null) || (matched.length() >= result.length())) {
				result = matched;
			}
		}
		return result;
	}


	public static String getImageStr(String imgFile) {
		InputStream inputStream = null;
		byte[] data = null;
		try {
			inputStream = new FileInputStream(imgFile);
			data = new byte[inputStream.available()];
			inputStream.read(data);
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 加密
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data);
	}



	public static void main(String[] args){
		System.out.println(getImageStr("E:\\desk\\1535654139829.jpg"));
	}


}
